package com.ruoyi.web.controller.api;

import java.util.HashMap;
import java.util.Map;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.OperatorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.ApiSignature;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.client.RestTemplate;

/**
 * 测试API签名认证控制器
 * 
 * @author ruoyi
 */
@ApiSignature
@RestController
@RequestMapping("/api/test")
public class TestApiController
{
    /**
     * 测试需要签名认证的GET请求
     */
    @GetMapping("/get")
    public AjaxResult testGet(String name, Integer age)
    {
        Map<String, Object> data = new HashMap<>();
        data.put("name", name);
        data.put("age", age);
        data.put("message", "测试签名认证成功");
        return AjaxResult.success(data);
    }
    
    /**
     * 测试不需要签名认证的GET请求
     */
    @ApiSignature(required = false)
    @GetMapping("/get/noauth")
    public AjaxResult testGetNoAuth(String name, Integer age)
    {
        Map<String, Object> data = new HashMap<>();
        data.put("name", name);
        data.put("age", age);
        data.put("message", "测试无签名认证成功");
        return AjaxResult.success(data);
    }


    private static final Logger logger = LoggerFactory.getLogger(TestApiController.class);

    @Autowired
    private RestTemplate restTemplate;
    @PostMapping("tp1")
    @Log(title = "测试mdc-python", businessType = BusinessType.OTHER,
            operatorType = OperatorType.HR_API, isSaveRequestData = true, isSaveResponseData = true)
    public AjaxResult testMdcPython(){


        try {
            // 处理业务逻辑...
           Map<String, Object> result = new HashMap<>();
            result.put("processed", true);
            result.put("original_size", 3);
            result.put("processed_time", System.currentTimeMillis());
            // 调用Python服务
            logger.info("准备调用Python服务");
            String pythonServiceUrl = "http://127.0.0.1:8000/api/analyze";

            // 设置跟踪头
            Map<String, Object> pythonRequest = new HashMap<>();
            pythonRequest.put("data", result);

            // 调用Python服务
            ResponseEntity<Map> pythonResponse = restTemplate.postForEntity(
                    pythonServiceUrl, pythonRequest, Map.class);

            // 处理Python服务返回结果
            logger.info("pythonResponse.getBody():{}",pythonResponse.getBody());

            logger.info("请求处理完成");
            return AjaxResult.success();

        } catch (Exception e) {
            logger.error("处理请求时发生错误", e);

            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("status", "error");
            errorResult.put("message", e.getMessage());

            return AjaxResult.error();
        }
    }
}